//
//  MZRouting.m
//  YiCuiMiaoZhang
//
//  Created by Sings on 2019/3/28.
//  Copyright © 2018 益萃网络科技（中国）有限公司. All rights reserved.
//

#import "MZRouting.h"
#import "NavServices.h"
#import <objc/runtime.h>
#import "NetWorkingServices.h"
#import "BaseRoutingDelegate.h"
#import "BZModule.h"
#import <Objection/Objection.h>

@implementation MZRouting

static MZRouting *_router = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _router = [[super allocWithZone:NULL] init];
        [[BaseRoutingDelegate sharedInstance] registerRouting];
    });
    return _router;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [MZRouting sharedInstance];
}

- (id)copyWithZone:(NSZone *)zone {
    return _router;
}

- (id)mutableCopy {
    return _router;
}

- (Class)getTableCellClassWithViewModel:(BaseViewModel *)viewModel {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicTableCellRouting;
    NSString *className = dic[NSStringFromClass([viewModel class])];
    NSParameterAssert([NSClassFromString(className) isSubclassOfClass:[BaseTableViewCell class]]);
    Class mataClass = objc_getMetaClass([className UTF8String]);
    NSParameterAssert(
        [mataClass instancesRespondToSelector:@selector(basetabelViewCellWithTableView:)] ||
        [mataClass
            instancesRespondToSelector:@selector(basetabelViewCellWithTableView:ForIndexPath:)]);
    return objc_getClass([className UTF8String]);
    ;
}

- (Class)getCollectCellClassWithViewModel:(BaseViewModel *)viewModel {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicCollectCellRouting;
    NSString *className = dic[NSStringFromClass([viewModel class])];
    NSParameterAssert(
        [NSClassFromString(className) isSubclassOfClass:[BaseCollectionViewCell class]]);
    Class mataClass = objc_getMetaClass([className UTF8String]);
    NSParameterAssert([mataClass instancesRespondToSelector:@selector
                                 (baseCollectionViewCellWithCollectionView:WithIndexPath:)]);
    return objc_getClass([className UTF8String]);
}

- (Class)getTableHeaderFooterWithViewModel:(BaseViewModel *)viewModel {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicTableHeaderFooterRouting;
    NSString *className = dic[NSStringFromClass([viewModel class])];
    NSParameterAssert([NSClassFromString(className) isSubclassOfClass:[BaseTableViewHeaderFooterView class]]);
    Class mataClass = objc_getMetaClass([className UTF8String]);
    NSParameterAssert([mataClass instancesRespondToSelector:@selector(baseTableViewHeaderFooterViewWithTableView:)]);
    return objc_getClass([className UTF8String]);
}

- (Class)getCollectionHeaderFooterWithViewModel:(BaseViewModel *)viewModel {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicCollectionHeaderFooterRouting;
    NSString *className = dic[NSStringFromClass([viewModel class])];
    NSParameterAssert([NSClassFromString(className) isSubclassOfClass:[BaseCollectionHeaderFooterView class]]);
    Class mataClass = objc_getMetaClass([className UTF8String]);
    NSParameterAssert([mataClass instancesRespondToSelector:@selector(baseCollectionHeaderFooterViewWithCollectionView:AndKind:AndIndexPath:)]);
    return objc_getClass([className UTF8String]);
}

- (NSArray<id> *) getObjectWihtProtocol:(Protocol *) protocol {
    NSString *key = NSStringFromProtocol(protocol);
    NSMutableArray *array = [BaseRegisterModel sharedInstance].mdicProtocolRouting[key];
    NSMutableArray *tempArray = [NSMutableArray array];
    if (array != nil) {
        for (NSString *named in array) {
            id object = [[JSObjection defaultInjector] getObject:protocol named:named];
            [tempArray addObject:object];
        }
    }
    return [tempArray copy];
}

// 临时添加 因为有些地方没用VM
- (NSDictionary *)noDeleteViewControllerDict {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicNoDeleteControllerRouting;
    return dic;
}

- (NSDictionary *)deleteViewControllerDict {
    NSDictionary *dic = @{

    };
    return dic;
}

- (Class)getViewControllerClassWithViewModel:(BaseControllerViewModel *)viewModel {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicViewControllerRouting;
    NSString *vcName  = dic[NSStringFromClass([viewModel class])];
    if (vcName != nil) {
        NSParameterAssert(
            [NSClassFromString(vcName) isSubclassOfClass:[BZBaseViewController class]]);
        NSParameterAssert(
            [NSClassFromString(vcName) instancesRespondToSelector:@selector(initWithViewModel:)]);
        return objc_getClass([vcName UTF8String]);
    } else {
        return nil;
    }
}

- (Class)getDeleteViewControllerClassWithViewModel:(BaseControllerViewModel *)viewModel {
    NSDictionary *dic = [self deleteViewControllerDict];
    NSString *vcName  = dic[NSStringFromClass([viewModel class])];
    if (vcName != nil) {
        return [self getViewControllerClassWithViewModel:viewModel];
    } else {
        return nil;
    }
}

- (BZBaseViewController *)getViewControllerWithViewModel:(BaseControllerViewModel *)viewModel {

    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicViewControllerRouting;

    NSString *vcName = dic[NSStringFromClass([viewModel class])];

    NSParameterAssert([NSClassFromString(vcName) isSubclassOfClass:[BZBaseViewController class]]);
    NSParameterAssert(
        [NSClassFromString(vcName) instancesRespondToSelector:@selector(initWithViewModel:)]);
    return [[NSClassFromString(vcName) alloc] initWithViewModel:viewModel];
}

- (BaseView *)getViewWithViewModel:(BaseViewModel *)viewModel {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicViewRouting;
    
    NSString *vcName = dic[NSStringFromClass([viewModel class])];
    
    NSParameterAssert([NSClassFromString(vcName) isSubclassOfClass:[BaseView class]]);
    NSParameterAssert(
        [NSClassFromString(vcName) instancesRespondToSelector:@selector(initWithViewModel:)]);
    return [[NSClassFromString(vcName) alloc] initWithViewModel:viewModel];
}

- (BaseButton *)getButtonWithViewModel:(BaseButtonModel *)viewModel {
    NSMutableDictionary *dic = [BaseRegisterModel sharedInstance].mdicButtonRouting;
    
    NSString *vcName = dic[NSStringFromClass([viewModel class])];
    
    NSParameterAssert([NSClassFromString(vcName) isSubclassOfClass:[BaseButton class]]);
    NSParameterAssert(
        [NSClassFromString(vcName) instancesRespondToSelector:@selector(initWithViewModel:)]);
    return [[NSClassFromString(vcName) alloc] initWithViewModel:viewModel];
}

- (NavServices *)navServices {
    if (_navServices == nil) { _navServices = [[NavServices alloc] init]; }
    return _navServices;
}

- (NetWorkingServices *)netWorkingServices {
    if (_netWorkingServices == nil) { _netWorkingServices = [[NetWorkingServices alloc] init]; }
    return _netWorkingServices;
}

@end
